home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / ply15dat.zip / TREE.C < prev    next >
C/C++ Source or Header  |  1992-09-19  |  5KB  |  163 lines

  1. /*
  2.  * tree.c - Creates a tree using Aono & Kunii's generation method.
  3.  *     (See IEEE CG&A May 1984).  A square polygon is placed beneath the
  4.  *     tree to act as a field.  No tree branch is clipped.  Seven light sources.
  5.  *
  6.  * Version:  2.2 (11/17/87)
  7.  * Author:  Eric Haines, 3D/Eye, Inc.
  8.  *
  9.  * SIZE_FACTOR determines the number of objects output.
  10.  *     Total objects = 2**(SF+1)-1 cones and spheres + 1 square polygon.
  11.  *
  12.  *     SIZE_FACTOR    # spheres       # cones    # squares
  13.  *        1             3               3         1
  14.  *        2             7               7         1
  15.  *        3            15              15         1
  16.  *
  17.  *       11          4095            4095         1
  18.  */
  19.  
  20. #include <stdio.h>
  21. #include <math.h>
  22. #include "def.h"
  23. #include "lib.h"
  24.  
  25. #define    OUTPUT_FORMAT        OUTPUT_CURVES
  26. #define    SIZE_FACTOR        9
  27.  
  28. /* the following affect the shape of the tree */
  29. #define    BR_ANGLE_0        40.0
  30. #define    BR_ANGLE_1        25.0
  31. #define    BR_CONTR_0        0.65
  32. #define    BR_CONTR_1        0.70
  33. #define    BR_DIAMETER        0.67
  34. #define    DIV_ANGLE        140.0
  35. #define    WIDTH_HEIGHTH_RATIO    0.15
  36.  
  37.  
  38. static    MATRIX    rst_mx[2] ;
  39.  
  40. /* grow tree branches recursively */
  41. static void
  42. grow_tree(MATRIX cur_mx, double scale, long depth)
  43. {
  44.     COORD4  apex, base, vec ;
  45.     long    i ;
  46.     MATRIX  new_mx ;
  47.  
  48.  
  49.     /* output branch */
  50.     SET_COORD4( vec, 0.0, 0.0, 0.0, 1.0 ) ;
  51.     lib_transform_coord( &base, &vec, cur_mx ) ;
  52.     base.w = scale * WIDTH_HEIGHTH_RATIO ;
  53.  
  54.     SET_COORD4( vec, 0.0, 0.0, 1.0, 1.0 ) ;
  55.     lib_transform_coord( &apex, &vec, cur_mx ) ;
  56.     apex.w = base.w * BR_DIAMETER ;
  57.  
  58.     lib_output_cylcone( &base, &apex, OUTPUT_CURVES ) ;
  59.     lib_output_sphere( &apex, OUTPUT_CURVES ) ;
  60.  
  61.     if ( depth > 0 ) {
  62.     --depth ;
  63.  
  64.     for ( i = 0; i < 2; ++i ) {
  65.         lib_matrix_multiply( new_mx, rst_mx[i], cur_mx ) ;
  66.         grow_tree(new_mx, scale * BR_DIAMETER, depth);
  67.     }
  68.     }
  69. }
  70.  
  71. /*
  72.  * Set up matrices for growth of each branch with respect to the
  73.  * parent branch, then grow each branch.
  74.  */
  75. static void
  76. create_tree()
  77. {
  78.     double  branch_angle, branch_contraction, divergence ;
  79.     long    i ;
  80.     MATRIX  ident_mx, temp1_mx, temp2_mx, tempr_mx, tempst_mx ;
  81.  
  82.  
  83.     for ( i = 0 ; i < 2 ; ++i ) {
  84.     if ( i == 0 ) {
  85.         branch_angle = BR_ANGLE_0 ;
  86.         divergence = 90.0 ;
  87.         branch_contraction = BR_CONTR_0 ;
  88.     }
  89.     else {
  90.         branch_angle = BR_ANGLE_1 ;
  91.         divergence = DIV_ANGLE + 90.0 ;
  92.         branch_contraction = BR_CONTR_1 ;
  93.     }
  94.  
  95.     /* rotate along X axis by branching angle */
  96.     lib_create_rotate_matrix( temp1_mx, X_AXIS, branch_angle*PI/180.0 ) ;
  97.  
  98.     /* rotate along Z axis by divergence angle */
  99.     lib_create_rotate_matrix( temp2_mx, Z_AXIS, divergence*PI/180.0 ) ;
  100.  
  101.     lib_matrix_multiply( tempr_mx, temp1_mx, temp2_mx ) ;
  102.  
  103.     /* include translation of branch, scaled */
  104.     lib_create_identity_matrix( tempst_mx ) ;
  105.     tempst_mx[0][0] = tempst_mx[1][1] = tempst_mx[2][2] =
  106.                             branch_contraction ;
  107.     tempst_mx[3][2] = 1.0 ;
  108.  
  109.     /* concatenate */
  110.     lib_matrix_multiply( rst_mx[i], tempr_mx, tempst_mx ) ;
  111.     }
  112.  
  113.     /* set up initial matrix */
  114.     lib_create_identity_matrix(ident_mx);
  115.     grow_tree(ident_mx, 1.0, SIZE_FACTOR);
  116. }
  117.  
  118. void
  119. main(int argc, char *argv[])
  120. {
  121.     COORD4  field[4] ;
  122.     COORD4  from, at, up, dir;
  123.     COORD4  light ;
  124.     COORD4  back_color, tree_color ;
  125.  
  126.    /* We are using Polyray */
  127.    lib_set_raytracer(OUTPUT_POLYRAY);
  128.  
  129.     /* output viewpoint */
  130.     SET_COORD( from, 4.5, 0.4, 2.0 ) ;
  131.     SET_COORD( at, 0.0, 0.0, 1.5 ) ;
  132.     SET_COORD( up, 0.0, 0.0, 1.0 ) ;
  133.     lib_output_viewpoint( &from, &at, &up, 45.0, 1.0, 1.0, 256, 256);
  134.  
  135.     /* output background color - UNC sky blue */
  136.     SET_COORD( back_color, 0.078, 0.361, 0.753 ) ;
  137.     lib_output_background_color( &back_color ) ;
  138.  
  139.     /* output light sources */
  140.     SET_COORD4( light, 10.0, 30.0, 40.0, 0.6 ) ;
  141.     lib_output_light( &light ) ;
  142.     SET_COORD4( light, -30.0, 40.0, 10.0, 0.6 ) ;
  143.     lib_output_light( &light ) ;
  144.     SET_COORD4( light, 50.0, 25.0, 20.0, 0.6 ) ;
  145.     lib_output_light( &light ) ;
  146.  
  147.     /* output field polygon - green */
  148.     SET_COORD( back_color, 0.2, 0.7, 0.2 ) ;
  149.     lib_output_color(&back_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0) ;
  150.     SET_COORD( field[0],  50.0,  50.0, 0.0 ) ;
  151.     SET_COORD( field[1], -50.0,  50.0, 0.0 ) ;
  152.     SET_COORD( field[2], -50.0, -50.0, 0.0 ) ;
  153.     SET_COORD( field[3],  50.0, -50.0, 0.0 ) ;
  154.     lib_output_polygon(4, field);
  155.  
  156.     /* set up tree color - brown */
  157.     SET_COORD( tree_color, 0.55, 0.4, 0.2 ) ;
  158.     lib_output_color( &tree_color, 0.2, 0.8, 0.0, 0.0, 0.0, 0.0, 0.0);
  159.  
  160.     /* create tree */
  161.     create_tree();
  162. }
  163.